package DAO;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Enumeration;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;

import sun.net.www.http.HttpClient;
import sun.net.www.protocol.http.HttpURLConnection;

import Beans.Aluno;
import Beans.Arquivo;
import Beans.Usuario;
import Listeners.ListenerFactory;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.filter.ClientFilter;
import com.sun.jersey.api.client.filter.ConnectionListenerFilter;
import com.sun.jersey.api.representation.Form;
import com.sun.jersey.multipart.BodyPart;
import com.sun.jersey.multipart.FormDataBodyPart;
import com.sun.jersey.multipart.FormDataMultiPart;
import com.sun.jersey.multipart.file.StreamDataBodyPart;
import com.sun.net.httpserver.Headers;
import com.sun.research.ws.wadl.Response;

public class ArquivoDAOImplementation implements ArquivoDAO {

	Client client;
	WebResource resource;

	public ArquivoDAOImplementation() {
		client = new Client();
	}

	@Override
	public String upload(Usuario usuario, String tipoDoc, InputStream in) {
		// TODO Auto-generated method stub
		String retorno = null;

		StreamDataBodyPart streamPart = new StreamDataBodyPart("file", in, tipoDoc, MediaType.APPLICATION_OCTET_STREAM_TYPE);
		
		FormDataMultiPart fdmp = new FormDataMultiPart();
		fdmp.bodyPart(streamPart);				
		
		FormDataBodyPart form = new FormDataBodyPart("Usuario", usuario, MediaType.APPLICATION_XML_TYPE);
		fdmp.bodyPart(form);
		
		Client client = new Client();		
		WebResource resource = client.resource("http://localhost:9090/Arquivo/Upload");
		retorno = resource.type(MediaType.MULTIPART_FORM_DATA_TYPE).post(String.class, fdmp);

		client.destroy();
		return retorno;
	}

	@Override
	public void download(Usuario usuario, String nomeArquivo) {
		// TODO Auto-generated method stub
		Client client = new Client();
		
		client.addFilter(new ConnectionListenerFilter(new ListenerFactory()));
		WebResource resource = client.resource("http://localhost:9090/Arquivo/Download/" + nomeArquivo);
		File retorno = resource.post(File.class, usuario);
		File novo = new File("C:/Users/Sergio_2/Desktop/Nova pasta (2)/" + nomeArquivo);
		retorno.renameTo(novo);
		client.destroy();
	}

	@Override
	public Arquivo [] buscarInfArquivos(Usuario usuario) {
		// TODO Auto-generated method stub
		resource = client.resource("http://localhost:9090/Arquivo/BuscarInfArquivos");
		Arquivo [] retorno = resource.post(Arquivo[].class, usuario);
		client.destroy();
		return retorno;
	}
}
